namespace net.podhola.gtd.controllers

import System
import System.Configuration
import System.Globalization
import System.Web
import System.Web.UI
import System.Web.UI.WebControls
import Castle.MonoRail.Framework
import net.podhola.gtd.finances


/*,Scaffolding( Expense )*/
[Layout("default")]
class ExpensesController(GTDParentController):

	public override def Title():		
		return "Expenses"

	def FindExpense( id as int ) as Expense:
		PropertyBag["Expense"] = Expense.Find( id )
		return PropertyBag["Expense"]

	public def Detail( id as int ):
		FindExpense( id )

	public def Edit( id as int ):
		FindExpense( id )
		
	public def Update( [DataBind("Expense")] expense as Expense ):
		accountid as int = Int32.Parse( Form[ "Expense.FromAccount" ] )
		expense.FromAccount = Account.Find( accountid )
		amount as double = Double.Parse( Form[ "Expense.Amount" ], NumberStyles.Currency )
		expense.Amount = amount
		expense.Save()
		RedirectToAction("Detail", { "id":expense.Id } )

	public def New():
		pass
		
	public def Create( Subject, Amount, EndDateTime, FromAccount ):
		expense = Expense()
		accountid as int = Int32.Parse( FromAccount )
		expense.FromAccount = Account.Find( accountid )
		
		amount as double = Double.Parse( Amount, NumberStyles.Currency )
		expense.Amount = amount
		expense.Subject = Subject
		expense.SpentOn  = DateTime.Parse( EndDateTime)			
		
		payingBill = ( Form[ "Bill.Id" ] + "" ) as string
		if payingBill.Length > 0:
			payingBillId = Int32.Parse( payingBill )
			expense.HasPayedBill = EnteredBill.Find( payingBillId )
		
		expense.Create()

		PropertyBag["Expense"] = expense
		
	public def Delete( id as int ):
		FindExpense( id ).Delete()
		RedirectToAction("Index" )
